//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "FilterSet.h"
#include "AddFilter.h"
#include "MainForm.h"
#include <Registry.hpp>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TfrmFilter *frmFilter;
//---------------------------------------------------------------------------
__fastcall TfrmFilter::TfrmFilter(TComponent* Owner)
	: TForm(Owner)
{
	this->filterstrlists = new TStringList();
	this->filterstrlists->Text = frmmain->RegFilterString.c_str();
	TStrings *ts = new TStringList();
	
	for(Integer i=1;i<=filterstrlists->Count;i++)
	{
		AnsiString s = filterstrlists->Strings[i-1];
		s = StringReplace(s,"|","\r\n",TReplaceFlags()<<rfReplaceAll);
		
		ts->Text = s;
		if(ts->Count == 3)
		{
			TListItem* li=this->ListView1->Items->Add();
			li->Caption = ts->Strings[0];
			li->SubItems->Add(ts->Strings[1]);
			li->SubItems->Add(ts->Strings[2]);
    }

	}
	delete ts;
	
}
//---------------------------------------------------------------------------
__fastcall TfrmFilter::~TfrmFilter()
{
	delete	filterstrlists;
}
//---------------------------------------------------------------------------

void __fastcall TfrmFilter::TrackBar1Change(TObject *Sender)
{
	int value = TrackBar1->Position;
	stxtBuffer->Caption = IntToStr(value) + " M";
}
//---------------------------------------------------------------------------


void __fastcall TfrmFilter::FormClose(TObject *Sender, TCloseAction &Action)
{
	Action = caFree;
}
//---------------------------------------------------------------------------

void __fastcall TfrmFilter::cmdCancelClick(TObject *Sender)
{
	Close();
}
//---------------------------------------------------------------------------

void __fastcall TfrmFilter::btnAddClick(TObject *Sender)
{
	TfrmAddFilter *frmAddFilter= new TfrmAddFilter(NULL,NULL);
	frmAddFilter->ShowModal();
}
//---------------------------------------------------------------------------
void __fastcall TfrmFilter::AddFiler(const char* name,const char* desc,const char* filterstr)
{
	TListItem* li = this->ListView1->Items->Add();
	li->Caption = name;
	li->SubItems->Add(desc);
	String s = AnsiString(name)+"|"+desc+"|"+filterstr;
	filterstrlists->Add(s);
}
//---------------------------------------------------------------------------
void __fastcall TfrmFilter::EditFilter(const char* name,const char* desc,const char* filterstr)
{
	TListItem* li = this->ListView1->Selected;
	li->Caption = name;
	li->SubItems->Strings[0]=desc;
	String s = AnsiString(name)+"|"+desc+"|"+filterstr;
	int i=this->ListView1->ItemIndex;
	filterstrlists->Strings[i]=s;
}
//---------------------------------------------------------------------------


void __fastcall TfrmFilter::Button2Click(TObject *Sender)
{
int i=ListView1->ItemIndex;
if(i>=0)
{
	filterstrlists->Delete(i);
	this->ListView1->Items->Delete(i);
}
}
//---------------------------------------------------------------------------

void __fastcall TfrmFilter::cmdOKClick(TObject *Sender)
{
	frmmain->RegFilterString = filterstrlists->GetText();
	frmmain->m_bPromisc = chkPromiss->Checked;
	AnsiString s = frmmain->RegFilterString.c_str();
	frmmain->formatFilterStr(filterstrlists,s);
	frmmain->FilterString = s.c_str();
	frmmain->m_MaxBuffer = this->TrackBar1->Position*1024*1024;
	SaveReg();
	Close();
}
//---------------------------------------------------------------------------
void __fastcall TfrmFilter::SaveReg()
{
	TRegistry *Registry = new TRegistry(KEY_ALL_ACCESS);
	try
	{
		Registry->RootKey = HKEY_LOCAL_MACHINE;
		// False because we do not want to create it if it doesn't exist
		if(Registry->OpenKey("Software\\adm\\neteye",true))
		{
			Registry->WriteString("ispromiss",chkPromiss->Checked ? "1" : "0");
			Registry->WriteString("FilterStr",filterstrlists->GetText());
			int maxbuffersize = this->TrackBar1->Position*1024*1024;
			Registry->WriteInteger("MaxBuffer",maxbuffersize);
			Registry->CloseKey();
		}
	}
	__finally
	{
		delete Registry;
	}
}
void __fastcall TfrmFilter::LoadReg()
{
	TRegistry *Registry = new TRegistry(KEY_ALL_ACCESS);
	try
	{
		Registry->RootKey = HKEY_LOCAL_MACHINE;
		// False because we do not want to create it if it doesn't exist
		if(Registry->OpenKey("Software\\adm\\neteye",true))
		{
			bool ispromiss = Registry->ReadString("ispromiss") != "0" ;
			chkPromiss->Checked = ispromiss;
		}
	}
	__finally
	{
		delete Registry;
	}
}
void __fastcall TfrmFilter::Button3Click(TObject *Sender)
{
int i=ListView1->ItemIndex;
if(i>=0)
{
	TfrmAddFilter *frmAddFilter= new TfrmAddFilter(NULL,filterstrlists->Strings[i]);
	frmAddFilter->ShowModal();
}
}
//---------------------------------------------------------------------------

void __fastcall TfrmFilter::FormCreate(TObject *Sender)
{
	LoadReg();	
}
//---------------------------------------------------------------------------

